

//加载方法
$(function (){
    $.ajax({
        url: "/DHMProceduring/queryByCheckTag1AndProcedure1",
        type:"get",
        success:function (data){
            $("#zongshu").html(data);
        }
    })

    //模糊查询的方法，生成表格
    $("#scdjTable").bootstrapTable({
        method:"get",
        contentType:"application/json;charset=utf-8",
        url: "/DHMProceduring/queryManufactureProcedureTag1",
        striped: true,
        pageNumber:1,
        pagination:true,
        sidePagination:"server",//设置服务端分页还是客户端分页
        pageSize:5,//页大小
        pageList:[3,5,9],//每页显示的条数
        queryParams:function (params) {
            return {
                pageNum: (params.offset / params.limit) + 1,
                pageSize:params.limit,
            }
        },
        columns:[
            {title:'生产派工单编号',field:'manufactureId',align:'center'},
            {title:'产品编号',field:'productId',align:'center'},
            {title:'产品名称',field:'productName',align:'center'},
            {title:'数量',field:'amount',align:'center'},
            {title:'生产计划序号组',field:'applyIdGroup',align:'center'},
            {title:'登记时间',field:'registerTime',align:'center'},
            {title:'复核',field:'id',align:'center',
                formatter:function (value,row,index) {
                    let btn="<button class='btn btn-info' onclick='registerBtn("+value+","+JSON.stringify(row)+")'>去复核</button>";
                    return btn;
                }
            },
        ]
    })

})

//点击去复核
let mMId=0;
let sGatherDetailsss=[];
function registerBtn(id,row){
    mMId=id;
    sGatherDetailsss[0]=row;
    //进入单页
    $(".div1").hide();
    $(".div2").show();
    $("#register").html("");
    $("#productId").html('');
    $("#productName").html("");
    $("#productDescribe").html("");
    $("#amount").html("");
    $("#remark").html("");
    $("#checkTime").html("");
    $("#registerTime").html("");
    $("#realModuleCostPriceSum").html("");
    $("#moduleCostPriceSum").html("");
    $("#checker").html('');
    $("#realLabourCostPriceSum").html("");
    $("#labourCostPriceSum").html("");
    $("#manufactureId").html("");
    //查询产品数量和信息
    $.ajax({
        url:"/DHMManufacture/byId/"+id,
        type:"get",
        dataType:"json",
        success:function (date){
            $("#register").html(date.register);
            $("#productId").html(date.productId);
            $("#productName").html(date.productName);
            $("#productDescribe").html(date.productDescribe);
            $("#amount").html(date.amount);
            $("#remark").html(date.remark);
            $("#checkTime").html(date.checkTime);
            $("#registerTime").html(date.registerTime);
            $("#realModuleCostPriceSum").html(date.realModuleCostPriceSum);
            $("#moduleCostPriceSum").html(date.moduleCostPriceSum);
            $("#checker").html(date.checker);
            $("#realLabourCostPriceSum").html(date.realLabourCostPriceSum);
            $("#labourCostPriceSum").html(date.labourCostPriceSum);
            $(".manufactureId").html(date.manufactureId);
            let data=date.mProcedures;
            let tr="";
            for (let i = 0; i < data.length; i++) {
                tr+="<tr>\n" +
                    "                    <td>"+data[i].procedureName+"</td>\n" +
                    "                    <td>"+data[i].labourHourAmount+"</td>\n" +
                    "                    <td>"+data[i].realLabourHourAmount+"</td>\n" +
                    "                    <td>"+data[i].subtotal+"</td>\n" +
                    "                    <td class='gongshi'>"+data[i].realSubtotal+"</td>\n" +
                    "                    <td>"+data[i].moduleSubtotal+"</td>\n" +
                    "                    <td class='wuliao'>"+data[i].realModuleSubtotal+"</td>\n" +
                    "                    <td>" ;

                if (data[i].procedureFinishTag=='0'){
                    tr+="";
                }else if (data[i].procedureFinishTag=='1'){
                    tr+="完成";
                }else if (data[i].procedureFinishTag=='2'){
                    tr+="未完成";
                }else if (data[i].procedureFinishTag=='3'){
                    tr+="<a href='#' onclick='shenhe("+JSON.stringify(data[i])+",this,"+i+")'>审核</a>";
                }
                tr+="</td>";

                if (data[i].procedureTransferTag=='0' && data[i].procedureFinishTag=='1'){
                    tr+="<td></td>";
                }else if (data[i].procedureTransferTag=='1' && data[i].procedureFinishTag=='1'){
                    tr+="<td>完成</td>";
                }else if (data[i].procedureTransferTag=='2' && data[i].procedureFinishTag=='1'){
                    if (i==0){
                        tr+="<td><a  data-toggle=\"modal\"  data-target=\"#myModal\"" +
                            " href='#' onclick='shenhejiaojie("+data[i].id+","+data[i].realAmount+",this,"+i+","+data[i].demandAmount+")'>交接复核</a></td>";
                    }else{
                        tr+="<td><a  data-toggle=\"modal\"  data-target=\"#myModal\"" +
                            " href='#' onclick='shenhejiaojie("+data[i].id+","+data[i].realAmount+",this,"+i+","+data[i-1].realAmount+")'>交接复核</a></td>";
                    }
                }else{
                    tr+="<td></td>";
                }

                tr+= "</tr>";
            }
            $("#tbodyForTr").html(tr);

        }
    })
}

//返回列表
function returnList(){
    $(".div1").show();
    $(".div2").hide();
    $(".div3").hide();
}

//点击登记
let mPId=0;
let mProceduring={};
let mProceduringModuling=[];
function shenhe(dui,that,index){
    mPId=dui.id;
    mProceduring=dui;

    $(".div1").hide();
    $(".div2").hide();
    $(".div3").show();

    $("#yygss").html('0.0');
    $("#gxmc").html(dui.procedureName);
    $("#sjgss").html(dui.labourHourAmount);

    $("#fuheren").html(parent.document.getElementsByClassName("user_name")[0].innerHTML);
    $("#fuheshijian").html('');

    $.ajax({
        url:"/DHMManufacture/queryByParentIdPM",
        type:"get",
        data:{
            parentId:dui.id
        },
        dataType:"json",
        success:function (data){
            mProceduringModuling=data;
            let tr="";
            for (let i = 0; i < data.length; i++) {
                tr+="<tr>\n" +
                    "                    <td>"+data[i].detailsNumber+"</td>\n" +
                    "                    <td>"+data[i].productName+"</td>\n" +
                    "                    <td>"+data[i].productId+"</td>\n" +
                    "                    <td>"+data[i].amount+"</td>\n" +
                    "                    <td>0</td>\n" +
                    "                    <td>0</td>\n" +
                    "                    <td><input class='form-control input-full' readonly name='amount'></td>\n" +
                    "                    <td></td>\n" +
                    "                </tr>";
            }
            $("#tbodyModuleForTr").html(tr);
            $.ajax({
                url:"/DHMProceduring/queryByParentIdProceduring",
                type:"get",
                data:{
                    parentId:dui.parentId,
                    procedureId:dui.procedureId,
                },
                dataType:"json",
                success:function (data){
                    $("#bcgss").val(data.labourHourAmount);
                    $("#fuzeren").html(data.procedureResponsiblePerson);

                    let mProcedureModulings = data.mProcedureModulings;
                    let amounts = $("input[name='amount']");
                    for (let i = 0; i < mProcedureModulings.length; i++) {
                        $(amounts[i]).val(mProcedureModulings[i].amount);
                    }
                }
            })
        }
    })
}


//返回工序
function returnGongXu(){
    $(".div1").hide();
    $(".div2").show();
    $(".div3").hide();
}

//点击提交登记按钮
function submitRegister(){
    mProceduring.labourHourAmount=$("#bcgss").val();
    mProceduring.subtotal=$("#bcgss").val()*mProceduring.costPrice;
    mProceduring.parentId=mMId;
    mProceduring.checker=$("#fuheren").html();
    let amounts = $("input[name='amount']");
    let price = 0;
    for (let i = 0; i < mProceduringModuling.length; i++) {
        price+=$(amounts[i]).val()*mProceduringModuling[i].costPrice;
    }

    let mManufacture={
        id:mMId,
        manufactureProcedureTag:"0",
    };
    let mProcedure={
        id: mPId,
        procedureFinishTag:"1",
        realLabourHourAmount:$("#bcgss").val(),
        realSubtotal:$("#bcgss").val()*mProceduring.costPrice,
        realModuleSubtotal:price,
    };
    if (confirm("确定已经完成了本工序的审核了吗？")){
        $.ajax({
            url:"/DHMProceduring/upMProceduringByParentIdAndProId",
            type:"put",
            contentType: "application/json;charset=utf-8",
            data:JSON.stringify(mProceduring),
            success:function (data){}
        })
        $.ajax({
            url:"/DHMManufacture/up",
            type:"put",
            contentType: "application/json;charset=utf-8",
            data:JSON.stringify(mManufacture),
            success:function (data){}
        })

        $.ajax({
            url:"/DHMManufacture/updateMProcedureById",
            type:"put",
            contentType: "application/json;charset=utf-8",
            data:JSON.stringify(mProcedure),
            success:function (data){
                $(".div1").show();
                $(".div2").hide();
                $(".div3").hide();
                $("#scdjTable").bootstrapTable("refresh");
            }
        })

    }
}

//交接审核
let flag=false;
function shenhejiaojie(id,amount,that,index,count){
    mPId=id;
    if ($(that).parent().parent().next().length==0){
        flag=true;
    }
    $("#realAmount").val(amount);
    if (index==0){
        $(".label1").show();
        $(".label2").hide();
        $(".tishi").html(count);
    }else{
        $(".label1").hide();
        $(".label2").show();
        $(".tishi").html(count);
    }
}

//确认提交交接登记
function submitYes(){
    let count = $(".tishi")[0].innerHTML;
    if(parseInt($("#realAmount").val())>parseInt(count)){
        $("#msg").html("数量不合理");
    }else if (parseInt($("#realAmount").val())<0){
        $("#msg").html("数量不能小于0")
    }else{
        sGatherDetailsss[0].personalValuue=$("#realAmount").val();
        sGatherDetailsss[0].id=null;
        sGatherDetailsss[0].parentId=null;
        sGatherDetailsss[0].realCostPrice='0';
        if (flag){
            let gongshi = $(".gongshi");
            let wuliao = $(".wuliao");
            let num = $("#amount").html();
            let zonggongshi=0;
            let zongwuliao=0;
            gongshi.each(function (i,e){
                zonggongshi+=parseInt($(e).html());
            })
            wuliao.each(function (i,e){
                zongwuliao+=parseInt($(e).html());
            })
            zongwuliao*=num;
            zonggongshi*=num;
            var sGather ={
                storer:parent.document.getElementsByClassName("user_name")[0].innerHTML,//入库人
                reason:"生产入库",//入库理由
                amountSum:$("#realAmount").val(),//总件数
                costPriceSum:'0.0',//总金额
                register:parent.document.getElementsByClassName("user_name")[0].innerHTML,//登记人
                remark:"派工单编号："+sGatherDetailsss[0].manufactureId,//备注
                sGatherDetails:sGatherDetailsss,//明细表
            }

            //添加入库表
            $.ajax({
                url:"/inputmanagerController/insertSGather",
                contentType : "application/json;charsetset=UTF-8",
                type:"post",
                async:true,
                data:JSON.stringify(sGather),
                success:function (data) {}
            })

            let mManufacture={
                id:mMId,
                manufactureProcedureTag:"2",
                testedAmount:$("#realAmount").val(),
                realModuleCostPriceSum:zongwuliao,
                realLabourCostPriceSum:zonggongshi,
            };
            let mProcedure={
                id: mPId,
                procedureTransferTag:"1",
                realAmount:$("#realAmount").val(),
            };
            $.ajax({
                url:"/DHMManufacture/up",
                type:"put",
                contentType: "application/json;charset=utf-8",
                data:JSON.stringify(mManufacture),
                success:function (data){}
            })
            $("#myModal").modal("hide");
            $.ajax({
                url:"/DHMManufacture/updateMProcedureById",
                type:"put",
                contentType: "application/json;charset=utf-8",
                data:JSON.stringify(mProcedure),
                success:function (data){
                    $(".div1").show();
                    $(".div2").hide();
                    $(".div3").hide();
                    $("#scdjTable").bootstrapTable("refresh");
                }
            })
        }else{
            let mManufacture={
                id:mMId,
                manufactureProcedureTag:"0",
            };
            let mProcedure={
                id: mPId,
                procedureTransferTag:"1",
                realAmount:$("#realAmount").val(),
            };
            $.ajax({
                url:"/DHMManufacture/up",
                type:"put",
                contentType: "application/json;charset=utf-8",
                data:JSON.stringify(mManufacture),
                success:function (data){}
            })
            $("#myModal").modal("hide");
            $.ajax({
                url:"/DHMManufacture/updateMProcedureById",
                type:"put",
                contentType: "application/json;charset=utf-8",
                data:JSON.stringify(mProcedure),
                success:function (data){
                    $(".div1").show();
                    $(".div2").hide();
                    $(".div3").hide();
                    $("#scdjTable").bootstrapTable("refresh");
                }
            })
        }
    }


}